package com.evol.controller;

import com.alibaba.fastjson.JSONObject;
import com.evol.domain.AjaxResult;
import com.evol.service.JsonFileService;
import com.evol.util.EvolTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.WebAuthenticationDetailsSource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * @author EvOL
 * @description
 * @date 2024-06-01 21:51
 **/
@Controller
public class HomeController extends BaseController{

    @Value("${evol.version}")
    private String version;
    @Value("${evol.name}")
    private String name;
    @Value("${evol.lang}")
    private String lang;

    @Autowired
    private JsonFileService jsonFileService;

    private final AuthenticationManager authenticationManager;

    public HomeController(AuthenticationManager authenticationManager) {
        this.authenticationManager = authenticationManager;
    }

    @GetMapping("/login")
    public String login(ModelMap model) {
        model.addAttribute("version", version);
        model.addAttribute("name", name);

        JSONObject json =jsonFileService.parseJsonString("config/"+lang+".json");
        for (Map.Entry<String, Object> entry : json.entrySet()) {
            String key = entry.getKey();
            Object value = entry.getValue();
            model.addAttribute(key.replace(" ",""), value);
        }
        return "login";
    }
    @PostMapping("/logins")
    @ResponseBody
    public AjaxResult loginAuth(@RequestParam String username, @RequestParam String password
            , HttpServletRequest request, HttpServletResponse response) throws IOException {
        try {
            UsernamePasswordAuthenticationToken authToken = new UsernamePasswordAuthenticationToken(username, password);
            authToken.setDetails(new WebAuthenticationDetailsSource().buildDetails(request));
            Authentication authentication = authenticationManager.authenticate(authToken);
            SecurityContextHolder.getContext().setAuthentication(authentication);
            return AjaxResult.success("登陆成功");
        } catch (AuthenticationException e) {
            return AjaxResult.error("登陆失败");
        }

    }
    @GetMapping("/logout")
    public void clearAuth(HttpServletRequest request, HttpServletResponse response) {
        // 清除用户的身份验证信息
        SecurityContextHolder.clearContext();
    }
    @GetMapping("/lang.json")
    @ResponseBody
    public String langjson() {
       JSONObject object= EvolTool.removeSpacesInKeys(jsonFileService.parseJsonString("config/"+lang+".json"));
       return object.toJSONString();
    }
    @GetMapping("/")
    public String home() {
        return "redirect:/index";
    }
    @GetMapping("/index")
    public String index(ModelMap model) {
        model.addAttribute("version", version);

        JSONObject json =jsonFileService.parseJsonString("config/"+lang+".json");
        for (Map.Entry<String, Object> entry : json.entrySet()) {
            String key = entry.getKey();
            Object value = entry.getValue();
            model.addAttribute(key.replace(" ",""), value);
        }
        return "index";
    }



}
